import sys
from PySide6.QtGui import QKeyEvent, QFont
from PySide6.QtWidgets import (QApplication, QTabWidget, QWidget, QVBoxLayout, QTextBrowser, QSlider, QGroupBox, QLabel, QHBoxLayout)
from PySide6.QtCore import Qt, Signal
from tab1 import Tab1
import time


class Dblgbx(QGroupBox):
    sld1 = Signal(int)
    sld2 = Signal(float)

    def __init__(self):
        super().__init__()
        self.gbx1 = Sldgrbx()
        self.gbx2 = Sldgrbx()
        self.gbx1.setTitle("自适应二值化邻域值")
        self.gbx1.sldr.setMinimum(1)
        self.gbx1.sldr.setMaximum(25)
        self.gbx1.sldr.setValue(5)

        # self.gbx1.sldr.setSingleStep(1)
        self.gbx1.sldr.valueChanged.connect(self.sld1vluchgd)
        self.gbx2.setTitle("图像压缩率")
        self.gbx2.sldr.setMinimum(1)
        self.gbx2.sldr.setMaximum(10)
        self.gbx2.sldr.setValue(3)
        self.gbx2.sldr.valueChanged.connect(self.sld2vluchgd)

        self.vlot = QVBoxLayout()
        self.vlot.addWidget(self.gbx1)
        self.vlot.addWidget(self.gbx2)
        self.setLayout(self.vlot)

    def sld1vluchgd(self, value):
        rlvlu = 2 * value + 1
        self.gbx1.lab.setText(str(rlvlu))
        self.sld1.emit(rlvlu)

    def sld2vluchgd(self, value):
        cmpsrat = value / 20
        self.gbx2.lab.setText(str(cmpsrat))
        self.sld2.emit(cmpsrat)

    def undermouse(self, agl):
        if self.gbx1.underMouse():
            self.gbx1.wheel(agl)
        elif self.gbx2.underMouse():
            self.gbx2.wheel(agl)

class Sldgrbx(QGroupBox):
    def __init__(self):
        super().__init__()
        self.sldr = QSlider(Qt.Horizontal)
        self.lab = QLabel()
        self.hblot = QHBoxLayout()
        self.setLayout(self.hblot)
        self.hblot.addWidget(self.sldr)
        self.hblot.addWidget(self.lab)
        self.sldr.setTickPosition(QSlider.TicksAbove)
        self.sldr.setTickInterval(1)
        self.ft = QFont()
        self.ft.setPointSize(14)
        self.lab.setFont(self.ft)

    # def wheel(self, agl):
    #     print(agl)
    #     if agl.y() > 0:
    #         self.sldr.setValue(self.sldr.value() + 1)
    #     else:
    #         self.sldr.setValue(self.sldr.value() - 1)

class Mainwindow(QTabWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(640, 480)
        self.move(0, 0)
        self.setWindowTitle("Point_pic")
        self.tab1 = Tab1()
        self.tab2 = QWidget()
        self.addTab(self.tab1, 'preview')
        self.addTab(self.tab2, 'output')
        self.setTabPosition(QTabWidget.East)
        self.tab1.resiz.connect(lambda siz: self.resize(siz.width(), siz.height()))
        self.setCurrentWidget(self.tab1)
        self.vlyt = QVBoxLayout()
        self.textbro = QTextBrowser()
        self.textbro.setStyleSheet("QTextBrowser {line-height: 50%;}")
        self.grbx = Dblgbx()

        self.tab2.setLayout(self.vlyt)
        self.tab1.widget.preview.connect(self.output)
        self.selftext = QFont()
        self.selftext.setPointSize(16)
        self.setFont(self.selftext)
        self.textfont = QFont()
        self.textfont.setFamily("Courier New")

    # def wheelEvent(self, event):
    #     if self.currentWidget() == self.tab2:
    #         agl = event.angleDelta()
    #         if self.grbx.underMouse():
    #             self.grbx.undermouse(agl)


    def cntdblgrpx(self):
        self.vlyt.addWidget(self.grbx)
        self.grbx.sld1.connect(self.sld1vluchgd)
        self.grbx.sld2.connect(self.sld2vluchgd)

    def startrun(self):
        start = time.time()
        self.tab1.enterpressed()
        finish = time.time()
        tm = round(finish - start, 2)
        print(f"operation cost {tm}s")
        print("-------------------------------------------------------------------------------------------------")

    def sld1vluchgd(self, value):
        self.tab1.widget.blocksz = value
        self.startrun()

    def sld2vluchgd(self, value):
        self.tab1.widget.ra = value
        # time.sleep(1)
        self.startrun()

    def keyPressEvent(self, event:QKeyEvent):
        if (event.key() == Qt.Key_Return and
            self.currentWidget() == self.tab1):
            self.cntdblgrpx()
            self.startrun()
            self.vlyt.addWidget(self.textbro)

                
    def output(self, str, sz):
        self.textbro.clear()
        self.textfont.setPixelSize(1)
        self.textbro.setFont(self.textfont)
        self.textbro.setPlainText(str)
        self.setCurrentWidget(self.tab2)
        
if __name__ == "__main__":
    app = QApplication([])
    window = Mainwindow()
    window.show()
    sys.exit(app.exec())